package com.example.ruiji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ruiji.common.CustomException;
import com.example.ruiji.mapper.CategoryMapper;
import com.example.ruiji.pojo.Category;
import com.example.ruiji.pojo.Dish;
import com.example.ruiji.pojo.Setmeal;
import com.example.ruiji.service.CategoryService;
import com.example.ruiji.service.DishService;
import com.example.ruiji.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
    private DishService dishService;
@Autowired
private SetmealService Set;

    @Override
    public void deletA(Long id) {
//我们需要判断 所删除的分类之下有没有关联
        //1.导入各自框架
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //2.进行查询
      dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int coud=dishService.count(dishLambdaQueryWrapper);
        if(coud>0)
        {
        throw new CustomException("当前分类关联了菜品 无法删除");
        }
        //3.自写拦截异常处理
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper1 = new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper1.eq(Setmeal::getCategoryId,id);

        int coud2=Set.count(dishLambdaQueryWrapper1);

        if(coud2>0)
        {
            throw new CustomException("当前分类关联了套餐 无法删除");
        }

        //无关联
        super.removeById(id);

    }
}
